﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Defecter.Gui.Access;
using DefectDBTypes;
using System.IO;



namespace Defector
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class addSoftware : Window
    {

        public SqlConnection con;
        SqlDataAdapter adapter = new SqlDataAdapter();

        public addSoftware()
        {
            InitializeComponent();
            datePicker.SelectedDate = DateTime.Now;
        }

        public void start()
        {
            loadCompanyData();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void createbtn_Click(object sender, RoutedEventArgs e)
        {
            //Check to see if form is completely filled out
            if (nameinput.Text != "" && versioninput.Text != "" && companybox.SelectedIndex > -1)
            {
                string sqlIns = "INSERT INTO Softwares (Software_Name, Software_Release_Date, Software_version_number, CompanyCompany_Id) VALUES (@Software_Name, @Software_Release_Date, @Software_version_number, @CompanyCompany_Id)";

                try
                {
                    con.Open();
                    SqlCommand cmdIns = new SqlCommand(sqlIns, con);
                    cmdIns.Parameters.AddWithValue("@Software_Name", nameinput.Text);
                    cmdIns.Parameters.AddWithValue("@Software_Release_Date", datePicker.SelectedDate);
                    cmdIns.Parameters.AddWithValue("@Software_version_number", versioninput.Text);
                    cmdIns.Parameters.AddWithValue("@CompanyCompany_Id", companybox.SelectedValue);
                    cmdIns.ExecuteNonQuery();
                    cmdIns.Dispose();
                    cmdIns = null;
                    con.Close();
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred while adding the software.");
                }
            }
            else
                MessageBox.Show("Please complete the form!");
        }

        //Load the company data into the company combo box
        public void loadCompanyData()
        {


            DataSet ds = new DataSet();
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT * FROM Companies";
                adapter.SelectCommand = cmd;
                adapter.Fill(ds, "Companies");

                DataRow nRow = ds.Tables["Companies"].NewRow();

                //Binding the data to the combobox.
                companybox.DataContext = ds.Tables["Companies"].DefaultView;

                //To display category name
                companybox.DisplayMemberPath =
                   ds.Tables["Companies"].Columns["Company_Name"].ToString();
                //To store the ID as hidden
                companybox.SelectedValuePath =
                   ds.Tables["Companies"].Columns["Company_Id"].ToString();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while loading company data.");
            }
        }
    }
}
